<?
    class _database
    {
        private static $connected = false;
        private static $link = null;

        public function  __construct() {
                return true;
        }

        private static function connect()
        {

            if(self::$connected) return self::$instance;
            $link = mysql_connect(_settings::$db['host'],_settings::$db['user'],_settings::$db['password']);
            $sel = mysql_select_db(_settings::$db['db'],$link);
            if(!$sel)
            {
                die('cant select database');
            }
            else
            {
                mysql_query('SET NAMES UTF8',$link);
                self::$connected = true;
                self::$link = $link;
                return true;
            }
        }

        public static function query($query,$nosave=false)
        {
            if(!self::$connected) self::connect();
            $start = microtime_float();
            $r = mysql_query($query);
            $execution_time = microtime_float() - $start;
            log::debug($query, $execution_time, true);
            if(!$r)
            {
                die($query.' '.mysql_error());
                return false;
            }
           
            return $r;
        }

        public static function torow($query,$m = MYSQL_ASSOC)
        {
            $r = self::query($query);
            $tmp = array();
            $row = mysql_fetch_array($r , $m);
            return $row?$row:false;
        }

        public static function toarray($query,$m = MYSQL_ASSOC)
        {
            $r = self::query($query);
            $tmp = array();
            while($row = mysql_fetch_array($r , $m))
            {
               $tmp[]  = $row;
            }
            return $tmp;
        }
    }
?>
